#include "stdafx.h"
#include "dom_visitor.h"

void DomVisitTestor::TestAccess(CefRefPtr<CefDOMDocument> document)
{
    printf("DomVisitTestor::TestAccess\n");
    printf(document->GetTitle().ToString().c_str());
    printf(document->GetBaseURL().ToString().c_str());
    printf("\n");
    CefRefPtr<CefDOMNode> headNode = document->GetHead();
    printf(headNode->GetName().ToString().c_str());
    printf(headNode->GetAsMarkup().ToString().c_str());
   
    char log[512] = { 0 };
    if (headNode->HasChildren())
    {
        CefRefPtr<CefDOMNode> childNode = headNode->GetFirstChild();

        do
        {
            sprintf_s(log, 256, "node name -%s, type-%d, value-%s\r\n",
                childNode->GetName().ToString().c_str(), childNode->GetType(), childNode->GetValue().ToString().c_str());
            printf(log);
        } while ((childNode = childNode->GetNextSibling()).get());
    }
    printf("\n");
    CefRefPtr<CefDOMNode> bodyNode = document->GetBody();
    //OutputDebugStringW(bodyNode->GetAsMarkup().ToWString().c_str());
    printf(bodyNode->GetAsMarkup().ToString().c_str());
    if (bodyNode->HasChildren())
    {
        CefRefPtr<CefDOMNode> childNode = bodyNode->GetFirstChild();
        do
        {
            sprintf_s(log, 256, "node name -%s, type-%d, value-%s\r\n",
                childNode->GetName().ToString().c_str(), childNode->GetType(), childNode->GetValue().ToString().c_str());
            printf(log);
        } while ((childNode = childNode->GetNextSibling()).get());
    }
}

void DomVisitTestor::TestModify(CefRefPtr<CefDOMDocument> document)
{
    OutputDebugStringW(L"DomVisitTestor::TestModify\r\n");
    CefRefPtr<CefDOMNode> bodyNode = document->GetBody();
    if (bodyNode->HasChildren())
    {
        CefRefPtr<CefDOMNode> childNode = bodyNode->GetFirstChild();
        wchar_t szLog[512] = { 0 };
        do {
            swprintf_s(szLog, 256, L"node name -%s,tagName-%s type-%d, value-%s\r\n",
                childNode->GetName().ToWString().c_str(),
                childNode->GetElementTagName().ToWString().c_str(),
                childNode->GetType(), childNode->GetValue());
            OutputDebugStringW(szLog);
            if (childNode->IsElement() && childNode->GetElementTagName() == "H1"
                && childNode->GetElementAttribute("id") == "hello")
            {
                CefRefPtr<CefDOMNode> textNode = childNode->GetFirstChild();
                swprintf_s(szLog, 512, L"found hello, text - %s\r\n", textNode->GetValue().ToWString().c_str());
                OutputDebugStringW(szLog);
                textNode->SetValue("Hello World Modified!");
                break;
            }
        } while ((childNode = childNode->GetNextSibling()).get());
    }

    CefRefPtr<CefDOMNode> hello = document->GetElementById("hello");
    if (hello.get())
    {
        hello->SetElementAttribute("align", "center");
        OutputDebugStringW(L"Change hello align\r\n");
    }
}

void DomVisitTestor::Visit(CefRefPtr<CefDOMDocument> document)
{
    TestAccess(document);
    /*  TestModify(document);*/
}
